package com.xjrsoft.module.customer.erp_model.discountActivities.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.customer.erp_model.discountActivities.entity.TActivity;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjrsoft.module.customer.erp_model.discountActivities.entity.TActivitydetails;
import com.xjrsoft.module.customer.erp_model.discountActivities.mapper.TActivitydetailsMapper;
import com.xjrsoft.module.customer.erp_model.discountActivities.dto.TActivityListDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import java.util.List;
import com.xjrsoft.module.customer.erp_model.discountActivities.mapper.TActivityMapper;
import com.xjrsoft.module.customer.erp_model.discountActivities.service.ITActivityService;
import com.xjrsoft.core.mp.base.BaseService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 *  服务实现类
 *
 * @author pp
 * @since 2021-10-29
 */
@Service
@AllArgsConstructor
public class TActivityServiceImpl extends BaseService<TActivityMapper, TActivity> implements ITActivityService {

	private TActivitydetailsMapper tActivitydetailsMapper;

	@Override
	public IPage<TActivity> getPageList(TActivityListDto pageListDto) {
		Wrapper<TActivity> wrapper = Wrappers.<TActivity>query().lambda()				.like(!StringUtil.isEmpty(pageListDto.getName()), TActivity::getName, pageListDto.getName())
				.eq(!StringUtil.isEmpty(pageListDto.getStatus()), TActivity::getStatus, pageListDto.getStatus())
				.ge(!StringUtil.isEmpty(pageListDto.getStartTime_Start()), TActivity::getStartTime, pageListDto.getStartTime_Start())
				.le(!StringUtil.isEmpty(pageListDto.getStartTime_End()), TActivity::getStartTime, pageListDto.getStartTime_End())
				.ge(!StringUtil.isEmpty(pageListDto.getF_CreatorTime_Start()), TActivity::getFCreatortime, pageListDto.getF_CreatorTime_Start())
				.le(!StringUtil.isEmpty(pageListDto.getF_CreatorTime_End()), TActivity::getFCreatortime, pageListDto.getF_CreatorTime_End());
		return this.page(ConventPage.getPage(pageListDto), wrapper);
	}

	@Override
	public String addTActivity(TActivity tActivity, List<TActivitydetails> tActivitydetailsList) {
		String tActivityId = IdWorker.get32UUID();
		tActivity.setId(tActivityId);
		tActivitydetailsList.forEach(tActivitydetails -> tActivitydetails.setActivityId(tActivityId));
		this.saveBatch(tActivitydetailsList, TActivitydetails.class, TActivitydetailsMapper.class);
		boolean isSuccess = this.save(tActivity);
		return isSuccess ? tActivityId : null;
	}

	@Override
	public boolean updateTActivity(String id, TActivity tActivity, List<TActivitydetails> tActivitydetailsList) {
		tActivity.setId(id);
		tActivitydetailsList.forEach(tActivitydetails -> tActivitydetails.setActivityId(id));
		tActivitydetailsMapper.delete(Wrappers.<TActivitydetails>query().lambda().eq(TActivitydetails::getActivityId, id));
		this.saveBatch(tActivitydetailsList, TActivitydetails.class, TActivitydetailsMapper.class);
		return this.updateById(tActivity);
	}

	public List<TActivitydetails> getTActivitydetailsByParentId(String parentId){
		Wrapper wrapper = Wrappers.<TActivitydetails>query().lambda().eq(TActivitydetails::getActivityId, parentId);
		return tActivitydetailsMapper.selectList(wrapper);
	}
}
